var record = require('../model/record');

exports.selectByEmployeeId = function(req, res, connection) {
	var employeeId = req.params['id'];
	var key = req.query.keyword;
	var cpage = req.query.cpage;
	var pageSize = req.query.pageSize;
	var begin = pageSize * (cpage - 1);
	var end = pageSize * cpage - 1;
	var keyData = {
		key: key,
		id: employeeId
	};
	//console.log('id:', employeeId);
	record.findByKeyAndId(connection, keyData, function(rows) {
		var data = [];
		var k = 0;
		for (var i = begin; i < end + 1; i++) {
			if (i < rows.length) {
				data[k] = rows[i];
				k++;
			}
		}
		var sendData = {
			data: data,
			count: rows.length
		}
		res.send(sendData);
	});
};

exports.selectByKeyAndId = function(req, res, connection) {
	var key = req.params['key'];
	var id = req.params['id'];
	var cpage = req.query.cpage;
	var pageSize = req.query.pageSize;
	var begin = pageSize * (cpage - 1);
	var end = pageSize * cpage - 1;
	var keyData = {
		key: key,
		id: id
	};
	//console.log('key:', key);
	record.findByKeyAndId(connection, keyData, function(data) {
		var data = [];
		var k = 0;
		for (var i = begin; i < end + 1; i++) {
			if (i < rows.length) {
				data[k] = rows[i];
				k++;
			}
		}
		var sendData = {
			data: data,
			count: rows.length
		}
		res.send(sendData);
	});
};